<!--
 * @Descripttion: 
 * @version: 
 * @Author: Ansel
 * @Date: 2020-03-04 18:26:54
 * @LastEditors: Ansel
 * @LastEditTime: 2020-04-22 23:29:03
 -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // function clone(obj, deep){
        //     if(Array.isArray(obj)){
        //         if(deep){
        //             let newArr = [];
        //             for(let i = 0;i < obj.length;i ++){
        //                 newArr.push(clone(obj[i],deep));
        //             }
        //             return newArr;
        //         }else{
        //             return obj.slice;
        //         }
        //     }else if(typeof obj === "object"){
        //        let newObj = {};
        //        for(const prop in obj){
        //            if(deep){
        //                 newObj[prop] = clone(obj[prop], deep);
        //            }else{
        //                 newObj[prop] = obj[prop];
        //            }
        //        }
        //        return newObj;
        //     }else{
        //         return obj;
        //     }
        // }

        function clone(obj,deep){
            if(Array.isArray(obj)){
                if(deep){
                    let newArr = [];
                    for(let i = 0;i < obj.length;i ++){
                        newArr.push(clone(obj[i],deep));
                    }
                    return newArr;
                }else{
                    return obj.slice;
                }
            }else if(typeof obj == "object"){
                let newObj = {};
                for(const prop in obj){
                    if(deep){
                        newObj[prop] = clone(obj[prop],deep);
                    }else{
                        newObj[prop] = obj[prop];
                    }
                }
                return newObj;
            }else{
                return obj;
            }
        }

        var obj = {
            x:1,
            y:2
        }

        // let Obj2 = JSON.parse(JSON.stringify(obj));
        // console.log(Obj2);
        // let newObj = clone(obj,true);
        // console.log(newObj);
        // newObj.x = 100;
        // console.log(newObj);
        // console.log(obj);

        
        // var obj2 = Object.create({},obj);
        // console.log(obj2);

        // $.extend(true,object1, object2)

        //newObject 即为深拷贝出来的对象
        // var newObject = $.extend(true , {} , object);
        
        class Node{
            constructor(value){
                this.value = value;
            }
        }
        let o = {a : 1, b : obj ,c : new Node(5)};
        let o2 = JSON.parse(JSON.stringify(o));

        // o.b = 2;
        console.log(o);
        console.log(o2);

    </script>
</body>
</html>